package nowcoder.course;

/**
 * @author fance
 * @date 2018/7/12 11:31
 */
public class Five {
    /**
     * 01背包练习题
     * @param w
     * @param v
     * @param n
     * @param cap
     * @return
     */
    public int maxValue(int[] w, int[] v, int n, int cap) {
        // write code here
        int[][] dp = new int[n + 1][cap + 1];
        for (int i = 0; i < n + 1; i++) {
            dp[i][0] = 0;
        }
        for (int i = 0; i < cap + 1; i++) {
            dp[0][i] = 0;
        }
        for (int i = 1; i < n + 1; i++) {
            for (int j = 1; j < cap + 1; j++) {
                if (w[i - 1] <= j) {
                    dp[i][j] = Math.max(dp[i - 1][j],dp[i - 1][j - w[i - 1]] + v[i - 1]);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        return dp[n][cap];
    }
}
